﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Magazine
	/// </summary>
	public partial class Magazine
	{
		public Magazine()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("MagazineId", "Magazine"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int MagazineId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Magazine");
			strSql.Append(" where MagazineId=@MagazineId");
			SqlParameter[] parameters = {
					new SqlParameter("@MagazineId", SqlDbType.Int,4)
			};
			parameters[0].Value = MagazineId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.Magazine model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Magazine(");
			strSql.Append("Name,ChannelId,Year,Month,Period,Unit1,Unit2,Chief,ISSN,CNISSN,MailCode,Pic,Chapter,Detail,Summary,CreateTime,UpdateTime,IsActive,Hits,IsTop)");
			strSql.Append(" values (");
			strSql.Append("@Name,@ChannelId,@Year,@Month,@Period,@Unit1,@Unit2,@Chief,@ISSN,@CNISSN,@MailCode,@Pic,@Chapter,@Detail,@Summary,@CreateTime,@UpdateTime,@IsActive,@Hits,@IsTop)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@ChannelId", SqlDbType.Int,4),
					new SqlParameter("@Year", SqlDbType.NVarChar,10),
					new SqlParameter("@Month", SqlDbType.NVarChar,10),
					new SqlParameter("@Period", SqlDbType.NVarChar,10),
					new SqlParameter("@Unit1", SqlDbType.NVarChar,50),
					new SqlParameter("@Unit2", SqlDbType.NVarChar,50),
					new SqlParameter("@Chief", SqlDbType.NVarChar,50),
					new SqlParameter("@ISSN", SqlDbType.NVarChar,50),
					new SqlParameter("@CNISSN", SqlDbType.NVarChar,50),
					new SqlParameter("@MailCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Pic", SqlDbType.NVarChar,50),
					new SqlParameter("@Chapter", SqlDbType.NText),
					new SqlParameter("@Detail", SqlDbType.NText),
					new SqlParameter("@Summary", SqlDbType.NText),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@IsTop", SqlDbType.Int,4)};
			parameters[0].Value = model.Name;
			parameters[1].Value = model.ChannelId;
			parameters[2].Value = model.Year;
			parameters[3].Value = model.Month;
			parameters[4].Value = model.Period;
			parameters[5].Value = model.Unit1;
			parameters[6].Value = model.Unit2;
			parameters[7].Value = model.Chief;
			parameters[8].Value = model.ISSN;
			parameters[9].Value = model.CNISSN;
			parameters[10].Value = model.MailCode;
			parameters[11].Value = model.Pic;
			parameters[12].Value = model.Chapter;
			parameters[13].Value = model.Detail;
			parameters[14].Value = model.Summary;
			parameters[15].Value = model.CreateTime;
			parameters[16].Value = model.UpdateTime;
			parameters[17].Value = model.IsActive;
			parameters[18].Value = model.Hits;
			parameters[19].Value = model.IsTop;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Magazine model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Magazine set ");
			strSql.Append("Name=@Name,");
			strSql.Append("ChannelId=@ChannelId,");
			strSql.Append("Year=@Year,");
			strSql.Append("Month=@Month,");
			strSql.Append("Period=@Period,");
			strSql.Append("Unit1=@Unit1,");
			strSql.Append("Unit2=@Unit2,");
			strSql.Append("Chief=@Chief,");
			strSql.Append("ISSN=@ISSN,");
			strSql.Append("CNISSN=@CNISSN,");
			strSql.Append("MailCode=@MailCode,");
			strSql.Append("Pic=@Pic,");
			strSql.Append("Chapter=@Chapter,");
			strSql.Append("Detail=@Detail,");
			strSql.Append("Summary=@Summary,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("UpdateTime=@UpdateTime,");
			strSql.Append("IsActive=@IsActive,");
			strSql.Append("Hits=@Hits,");
			strSql.Append("IsTop=@IsTop");
			strSql.Append(" where MagazineId=@MagazineId");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@ChannelId", SqlDbType.Int,4),
					new SqlParameter("@Year", SqlDbType.NVarChar,10),
					new SqlParameter("@Month", SqlDbType.NVarChar,10),
					new SqlParameter("@Period", SqlDbType.NVarChar,10),
					new SqlParameter("@Unit1", SqlDbType.NVarChar,50),
					new SqlParameter("@Unit2", SqlDbType.NVarChar,50),
					new SqlParameter("@Chief", SqlDbType.NVarChar,50),
					new SqlParameter("@ISSN", SqlDbType.NVarChar,50),
					new SqlParameter("@CNISSN", SqlDbType.NVarChar,50),
					new SqlParameter("@MailCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Pic", SqlDbType.NVarChar,50),
					new SqlParameter("@Chapter", SqlDbType.NText),
					new SqlParameter("@Detail", SqlDbType.NText),
					new SqlParameter("@Summary", SqlDbType.NText),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@IsTop", SqlDbType.Int,4),
					new SqlParameter("@MagazineId", SqlDbType.Int,4)};
			parameters[0].Value = model.Name;
			parameters[1].Value = model.ChannelId;
			parameters[2].Value = model.Year;
			parameters[3].Value = model.Month;
			parameters[4].Value = model.Period;
			parameters[5].Value = model.Unit1;
			parameters[6].Value = model.Unit2;
			parameters[7].Value = model.Chief;
			parameters[8].Value = model.ISSN;
			parameters[9].Value = model.CNISSN;
			parameters[10].Value = model.MailCode;
			parameters[11].Value = model.Pic;
			parameters[12].Value = model.Chapter;
			parameters[13].Value = model.Detail;
			parameters[14].Value = model.Summary;
			parameters[15].Value = model.CreateTime;
			parameters[16].Value = model.UpdateTime;
			parameters[17].Value = model.IsActive;
			parameters[18].Value = model.Hits;
			parameters[19].Value = model.IsTop;
			parameters[20].Value = model.MagazineId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int MagazineId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Magazine ");
			strSql.Append(" where MagazineId=@MagazineId");
			SqlParameter[] parameters = {
					new SqlParameter("@MagazineId", SqlDbType.Int,4)
			};
			parameters[0].Value = MagazineId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string MagazineIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Magazine ");
			strSql.Append(" where MagazineId in ("+MagazineIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Magazine GetModel(int MagazineId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 MagazineId,Name,ChannelId,Year,Month,Period,Unit1,Unit2,Chief,ISSN,CNISSN,MailCode,Pic,Chapter,Detail,Summary,CreateTime,UpdateTime,IsActive,Hits,IsTop from Magazine ");
			strSql.Append(" where MagazineId=@MagazineId");
			SqlParameter[] parameters = {
					new SqlParameter("@MagazineId", SqlDbType.Int,4)
			};
			parameters[0].Value = MagazineId;

			Maticsoft.Model.Magazine model=new Maticsoft.Model.Magazine();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["MagazineId"]!=null && ds.Tables[0].Rows[0]["MagazineId"].ToString()!="")
				{
					model.MagazineId=int.Parse(ds.Tables[0].Rows[0]["MagazineId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Name"]!=null && ds.Tables[0].Rows[0]["Name"].ToString()!="")
				{
					model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ChannelId"]!=null && ds.Tables[0].Rows[0]["ChannelId"].ToString()!="")
				{
					model.ChannelId=int.Parse(ds.Tables[0].Rows[0]["ChannelId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Year"]!=null && ds.Tables[0].Rows[0]["Year"].ToString()!="")
				{
					model.Year=ds.Tables[0].Rows[0]["Year"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Month"]!=null && ds.Tables[0].Rows[0]["Month"].ToString()!="")
				{
					model.Month=ds.Tables[0].Rows[0]["Month"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Period"]!=null && ds.Tables[0].Rows[0]["Period"].ToString()!="")
				{
					model.Period=ds.Tables[0].Rows[0]["Period"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Unit1"]!=null && ds.Tables[0].Rows[0]["Unit1"].ToString()!="")
				{
					model.Unit1=ds.Tables[0].Rows[0]["Unit1"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Unit2"]!=null && ds.Tables[0].Rows[0]["Unit2"].ToString()!="")
				{
					model.Unit2=ds.Tables[0].Rows[0]["Unit2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Chief"]!=null && ds.Tables[0].Rows[0]["Chief"].ToString()!="")
				{
					model.Chief=ds.Tables[0].Rows[0]["Chief"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ISSN"]!=null && ds.Tables[0].Rows[0]["ISSN"].ToString()!="")
				{
					model.ISSN=ds.Tables[0].Rows[0]["ISSN"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CNISSN"]!=null && ds.Tables[0].Rows[0]["CNISSN"].ToString()!="")
				{
					model.CNISSN=ds.Tables[0].Rows[0]["CNISSN"].ToString();
				}
				if(ds.Tables[0].Rows[0]["MailCode"]!=null && ds.Tables[0].Rows[0]["MailCode"].ToString()!="")
				{
					model.MailCode=ds.Tables[0].Rows[0]["MailCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Pic"]!=null && ds.Tables[0].Rows[0]["Pic"].ToString()!="")
				{
					model.Pic=ds.Tables[0].Rows[0]["Pic"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Chapter"]!=null && ds.Tables[0].Rows[0]["Chapter"].ToString()!="")
				{
					model.Chapter=ds.Tables[0].Rows[0]["Chapter"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Detail"]!=null && ds.Tables[0].Rows[0]["Detail"].ToString()!="")
				{
					model.Detail=ds.Tables[0].Rows[0]["Detail"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Summary"]!=null && ds.Tables[0].Rows[0]["Summary"].ToString()!="")
				{
					model.Summary=ds.Tables[0].Rows[0]["Summary"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateTime"]!=null && ds.Tables[0].Rows[0]["UpdateTime"].ToString()!="")
				{
					model.UpdateTime=DateTime.Parse(ds.Tables[0].Rows[0]["UpdateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsActive"]!=null && ds.Tables[0].Rows[0]["IsActive"].ToString()!="")
				{
					model.IsActive=int.Parse(ds.Tables[0].Rows[0]["IsActive"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Hits"]!=null && ds.Tables[0].Rows[0]["Hits"].ToString()!="")
				{
					model.Hits=int.Parse(ds.Tables[0].Rows[0]["Hits"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsTop"]!=null && ds.Tables[0].Rows[0]["IsTop"].ToString()!="")
				{
					model.IsTop=int.Parse(ds.Tables[0].Rows[0]["IsTop"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select MagazineId,Name,ChannelId,Year,Month,Period,Unit1,Unit2,Chief,ISSN,CNISSN,MailCode,Pic,Chapter,Detail,Summary,CreateTime,UpdateTime,IsActive,Hits,IsTop ");
			strSql.Append(" FROM Magazine ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" MagazineId,Name,ChannelId,Year,Month,Period,Unit1,Unit2,Chief,ISSN,CNISSN,MailCode,Pic,Chapter,Detail,Summary,CreateTime,UpdateTime,IsActive,Hits,IsTop ");
			strSql.Append(" FROM Magazine ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Magazine ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.MagazineId desc");
			}
			strSql.Append(")AS Row, T.*  from Magazine T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Magazine";
			parameters[1].Value = "MagazineId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

